<?
  require('class.ADfunctions.php');
  
  $input_file = $argv[1];
  $output_file = $argv[2];
  $type = $argv[3];
  $people_dn = "ou=people,o=lafayette";
  $course_dn = "ou=courses,o=lafayette";
  
  $explode = ($type == "student") ? "," : "|";
  
  if($data = file_get_contents($input_file)) {
    $lines = explode("\n", $data);
    $header_row = str_replace("#","",$lines[0]);
    $header_items = explode($explode, $header_row);
    $elements = array();
    $messages = array();
    foreach($header_items as $key => $item) {
      $elements[$item] = $key;
    }
    
    for($i=1;$i<count($lines);$i++) {
      $items = explode($explode, $lines[$i]);
      //switch($type) {
        if($type == "student") {
          // Student import
          $objectclasses = array("person","top","inetOrgPerson","organizationalPerson");
          $messages[] = "dn: cn=".$items[$elements["username"]].",".$people_dn;
          $messages[] = "changetype: add";
          $messages[] = "cn: ".$items[$elements["username"]];
          $messages[] = "givenName: ".$items[$elements["firstname"]];
          $messages[] = "sn: ".$items[$elements["lastname"]];
          $messages[] = "mail: ".$items[$elements["email"]];
          $messages[] = "userPassword: {SHA}".base64_encode(sha1($items[$elements["password"]], true));
          foreach($objectclasses as $oc) {
            $messages[] = "objectclass: $oc";
          }        
         // $messages[] = "-";
          $messages[] = "";
        }
        if($type == "course") {
          // Course import
          $objectclasses = array("top", "posixGroup");
          
          // Create courses
          // Determine department
          $dept_split = split('[[:digit:]]',$items[$elements["shortname"]]);
          $department = $dept_split[0];
          
          // Determine year
          $year = $items[$elements["startyear"]];
          
          // Determine term
          $term_split = explode("_", $items[$elements["shortname"]]);
          $term = $term_split[1];
          
          // Start student course branch
          $cns = array("Students","Instructors");
          foreach($cns as $cn) {
            $outree = array($cn,$department,$year,$term);
            $ouloc = array();
            foreach($outree as $ou) {
              array_unshift($ouloc, $ou);
              print_r(ADfunctions::getField(implode(",",$ouloc),"dn","cn"));
            }
            $messages[] = "dn: cn=".$items[$elements["idnumber"]].",ou=$term,ou=$year,ou=$department,ou=$cn,".$course_dn;
            $messages[] = "changetype: add";
            $messages[] = "cn: ".$items[$elements["idnumber"]];
            $messages[] = "displayName: ".$term_split[0];
            $messages[] = "description: ".$items[$elements["fullname"]];
            foreach($objectclasses as $oc) {
              $messages[] = "objectclass: $oc;";
            }
            $messages[] = "";
          }         
        }
    }
    
    if($fp = fopen($output_file, "w")) {
      foreach($messages as $message) {
        fwrite($fp, $message."\n");
      }
      fclose($fp);
    } else die ("Failed to open output file.");
          
  } else die ("Failed to open input file.");
  
  echo "Done.\n\n";
?>